//
//  OrderEvaluationViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/10.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "OrderEvaluationViewController.h"

#import "WTKStarView.h"

@interface OrderEvaluationViewController ()<UITextViewDelegate>

@property (nonatomic,strong) UIImageView *goodsImgV;//商品图片

@property (nonatomic,strong) UILabel *goodsNameLB;//商品名称

@property (nonatomic,strong) UILabel *leftInfoLB;//

@property (nonatomic,strong) WTKStarView *starView;//星级

@property (nonatomic,strong) UITextView *comment_TV;//评论 TV

@property (nonatomic,strong) UIButton *submitBtn;//提交评价btn

@end

static NSString *tvPlaceHolders = @"评价商品，可以帮助更多想买的人";

@implementation OrderEvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"用户评价";
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self initUI];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.goodsNameLB.text = self.goodsName;
        [self.goodsImgV sd_setImageWithURL:[NSURL URLWithString:self.goodsLogo]];
    });
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.goodsImgV];
    [self.view addSubview:self.goodsNameLB];
    [self.view addSubview:self.leftInfoLB];
    [self.view addSubview:self.starView];
    [self.view addSubview:self.comment_TV];
    [self.view addSubview:self.submitBtn];
}
#pragma mark - lazy
- (UIImageView *)goodsImgV{
    if (!_goodsImgV) {
        _goodsImgV = [[UIImageView alloc] initWithFrame:CGRectMake(15, self.topNavView.bottom + 20, 50, 50)];
        _goodsImgV.clipsToBounds = YES;
        _goodsImgV.layer.cornerRadius = 4;
    }
    return _goodsImgV;
}
- (UILabel *)goodsNameLB{
    if (!_goodsNameLB) {
        _goodsNameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.right+12, self.goodsImgV.top+4, ScreenWidth - 92, 42)];
        _goodsNameLB.numberOfLines = 2;
        _goodsNameLB.textAlignment = NSTextAlignmentLeft;
        _goodsNameLB.font = FontSet(13);
        _goodsNameLB.textColor = MainPureColor(85);
    }
    return _goodsNameLB;
}
- (UILabel *)leftInfoLB{
    if (!_leftInfoLB) {
        _leftInfoLB = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsImgV.left, self.goodsImgV.bottom+16, 70, 20)];
        _leftInfoLB.textAlignment = NSTextAlignmentLeft;
        _leftInfoLB.font = FontSetWithWeight(14, 0.3f);
        _leftInfoLB.textColor = MainPureColor(85);
        _leftInfoLB.text = @"描述相符";
    }
    return _leftInfoLB;
}
///星级评分
-(WTKStarView *)starView{
    if (!_starView) {
        _starView = [[WTKStarView alloc] initWithFrame:CGRectMake(self.leftInfoLB.right+16, self.leftInfoLB.top-2, 150, 25) starSize:CGSizeZero withStyle:WTKStarTypeInteger];
        [_starView setStar:5.0];
        _starView.isTouch = YES;
    }
    return _starView;
}
- (UITextView *)comment_TV{
    if (!_comment_TV) {
        _comment_TV = [[UITextView alloc] initWithFrame:CGRectMake(15, self.starView.bottom+30, ScreenWidth-30, 160)];
        _comment_TV.backgroundColor = MainPureColor(246);
        _comment_TV.clipsToBounds = YES;
        _comment_TV.layer.cornerRadius = 4;
        _comment_TV.delegate = self;
        _comment_TV.text = tvPlaceHolders;
        _comment_TV.textColor = MainGrayTextColor;
        _comment_TV.font = FontSet(13);
    }
    return _comment_TV;
}
- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.frame = CGRectMake(ScreenWidth/2-90, ScreenHeight-kSafe_Bottom - 70, 180, 40);
        _submitBtn.backgroundColor = MainThemeColor;
        _submitBtn.clipsToBounds = YES;
        _submitBtn.layer.cornerRadius = 4;
        [_submitBtn setTitle:@"提交评价" forState:normal];
        [_submitBtn setTitleColor:UIColor.whiteColor forState:normal];
        [_submitBtn.titleLabel setFont:FontSetWithWeight(17, 0.3f)];
        [_submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

#pragma mark - SEL
-(void)submitClick{
    if ([self.comment_TV.text isEqualToString:tvPlaceHolders] || self.comment_TV.text.length <= 0) {
        [[XNProgressHUD shared] showWithTitle:@"请填写商品评价"];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"comment"] = self.comment_TV.text;
    params[@"star"] = @(self.starView.star);
    [NetWorkingOrder submitCommentWithItemId:self.itemID withParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"评价成功！"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self popAndPostRefreshNotificationWithListType:4];
        });
    }];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([self.comment_TV.text isEqualToString:tvPlaceHolders]) {
        [self.comment_TV setText:@""];
        self.comment_TV.textColor = MainBlackTextColor;
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if ([self.comment_TV.text isEqualToString:tvPlaceHolders] || self.comment_TV.text.length <= 0) {
        [self.comment_TV setText:tvPlaceHolders];
        self.comment_TV.textColor = MainGrayTextColor;
    }else{
        self.comment_TV.textColor = MainBlackTextColor;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//发送通知让订单列表刷新 并返回上级界面
-(void)popAndPostRefreshNotificationWithListType:(NSInteger)listType{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
        NSDictionary *dic = @{@"listType":@(listType)};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOrderListNotification" object:nil userInfo:dic];
    });
}





@end





